/******************** (C) COPYRIGHT 2020 SONiX *******************************
* COMPANY:			SONiX
* DATE:					2020/06
* AUTHOR:				SA1
* IC:						SN32F280/SN32F290
* DESCRIPTION:	Utility related functions.
*____________________________________________________________________________
* REVISION	Date				User		Description
* 1.0				2020/06/19	SA1			1. First release
*
*____________________________________________________________________________
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS TIME TO MARKET.
* SONiX SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL 
* DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF SUCH SOFTWARE
* AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN 
* IN CONNECTION WITH THEIR PRODUCTS.
*****************************************************************************/

/*_____ I N C L U D E S ____________________________________________________*/
#include "Utility.h"

/*_____ D E C L A R A T I O N S ____________________________________________*/


/*_____ D E F I N I T I O N S ______________________________________________*/
#define	UTILITY_10US_DELAY		80>>SN_SYS0->AHBCP


/*_____ M A C R O S ________________________________________________________*/


/*_____ F U N C T I O N S __________________________________________________*/
/*****************************************************************************
* Function		: UT_INT_DelayNx10us
* Description	: Delay N x 10 us @ Fcpu = 12~48MHz
* Input			: N
* Output		: None
* Return		: None
* Note			: May have some deviation (+/-20%)
*							This Function work in INTERRPUT 
*****************************************************************************/
void UT_INT_DelayNx10us (uint32_t N)
{
	N*=UTILITY_10US_DELAY;			//10us @ Fcpu = 12~48MHz
  while (N--);
}


/*****************************************************************************
* Function		: UT_INT_DelayNms
* Description	: Delay N ms @ Fcpu = Fcpu = 12~48MHz
* Input			: N
* Output		: None
* Return		: None
* Note			: May have some deviation (+/-20%)
*							This Function work in INTERRPUT 
*****************************************************************************/
void UT_INT_DelayNms (uint32_t N)
{
	while (N--)
	{
		SN_WDT->FEED = 0x5AFA55AA;
		UT_INT_DelayNx10us(100);	//10us x 100 = 1ms
	}
}


/*****************************************************************************
* Function		: UT_Main_DelayNx10us
* Description	: Delay N x 10 us @ Fcpu = Fcpu = 12~48MHz
* Input			: N
* Output		: None
* Return		: None
* Note			: May have some deviation (+/-20%)
*							This Function work in Main Loop 
*****************************************************************************/
void UT_MAIN_DelayNx10us (uint32_t N)
{
	N *= UTILITY_10US_DELAY;			//10us @ Fcpu = 12~48MHz
  while (N--);
}


/*****************************************************************************
* Function		: UT_Main_DelayNms
* Description	: Delay N ms @ Fcpu = 12~48MHz
* Input			: N
* Output		: None
* Return		: None
* Note			: May have some deviation (+/-20%)
*							This Function work in Main Loop 
*****************************************************************************/
void UT_MAIN_DelayNms (uint32_t N)
{
	while (N--)
	{
		SN_WDT->FEED = 0x5AFA55AA;
		UT_MAIN_DelayNx10us(100);	//10us x 100 = 1ms
	}
}

/*****************************************************************************
* Function		: UT_Main_DelayNms
* Description	: Delay N ms @ Fcpu = 12~48MHz
* Input			: N
* Output		: None
* Return		: None
* Note			: May have some deviation (+/-20%)
*							This Function work in Main Loop 
*****************************************************************************/
void UT_Delay_Nms(uint32_t N)
{
	while (N--)
	{
		SN_WDT->FEED = 0x5AFA55AA;
		UT_MAIN_DelayNx10us(100);	//10us x 100 = 1ms
	}
}

/*****************************************************************************
* Function		: UT_Delay_2us
* Description	: Delay 2 us
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void UT_Delay_2us(void)
{
	uint16_t i;

	for (i=13; i!=0; i--);	//Fcpu = 48MHz
}

/*****************************************************************************
* Function		: UT_Delay_1us
* Description	: Delay 1 us
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void UT_Delay_1us(void)
{
	uint16_t i;

	for (i=3; i!=0; i--);	//Fcpu = 48MHz
}
/*****************************************************************************
* Function		: UT_Delay_Nus
* Description	: Delay N us
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void UT_Delay_Nus(uint16_t N)
{
	uint16_t i;

	for (i=(N/2)-1; i!=0; i--)	//Fcpu = 48MHz
		UT_Delay_2us();
}


